package cn.catsdad.auth.handle;

import cn.catsdad.auth.service.TokenService;
import cn.catsdad.constant.iConstant.IResponseStatus;
import cn.catsdad.util.ServletUtil;
import cn.catsdad.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 账号退出处理类，清除redis缓存等
 *
 * @author xuyonglong
 * @version V1.0 2023/4/14
 */
@Component
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler {

    private TokenService tokenService;

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (loginUser != null) {
            tokenService.delCacheUser(loginUser.getToken());
        }
        ServletUtil.renderString(response, "退出成功！", IResponseStatus.SUCCESS);
    }

    @Autowired
    public void setTokenService(TokenService tokenService) {
        this.tokenService = tokenService;
    }
}
